<?php
/**
 * API测试脚本
 * 用于测试后端API接口是否正常工作
 */

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../helpers/functions.php';

class ApiTest {
    private $baseUrl;
    private $token;
    
    public function __construct($baseUrl = 'http://localhost/ksys/backend/api') {
        $this->baseUrl = $baseUrl;
    }
    
    public function runAllTests() {
        echo "=== 健康营养食谱API测试 ===\n\n";
        
        $this->testApiStatus();
        $this->testUserRegistration();
        $this->testUserLogin();
        $this->testFoodApi();
        $this->testRecipeApi();
        
        echo "\n=== 测试完成 ===\n";
    }
    
    private function httpRequest($url, $method = 'GET', $data = null, $headers = []) {
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        
        if ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
            if ($data) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
                $headers[] = 'Content-Type: application/json';
            }
        }
        
        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        return [
            'code' => $httpCode,
            'response' => json_decode($response, true)
        ];
    }
    
    private function testApiStatus() {
        echo "1. 测试API服务状态...\n";
        
        $result = $this->httpRequest($this->baseUrl . '/');
        
        if ($result['code'] === 200 && isset($result['response']['code']) && $result['response']['code'] === 200) {
            echo "   ✅ API服务正常\n";
        } else {
            echo "   ❌ API服务异常: " . json_encode($result) . "\n";
        }
    }
    
    private function testUserRegistration() {
        echo "2. 测试用户注册...\n";
        
        $testPhone = '138' . rand(10000000, 99999999);
        $data = [
            'phone' => $testPhone,
            'password' => 'test123456',
            'nickname' => '测试用户'
        ];
        
        $result = $this->httpRequest($this->baseUrl . '/user/register', 'POST', $data);
        
        if ($result['code'] === 200 && isset($result['response']['code']) && $result['response']['code'] === 200) {
            echo "   ✅ 用户注册成功\n";
        } else {
            echo "   ⚠️ 用户注册测试: " . ($result['response']['message'] ?? '未知错误') . "\n";
        }
    }
    
    private function testUserLogin() {
        echo "3. 测试用户登录...\n";
        
        $data = [
            'phone' => '13800000000',
            'password' => '123456'
        ];
        
        $result = $this->httpRequest($this->baseUrl . '/user/login', 'POST', $data);
        
        if ($result['code'] === 200 && isset($result['response']['code']) && $result['response']['code'] === 200) {
            $this->token = $result['response']['data']['token'] ?? null;
            echo "   ✅ 用户登录成功\n";
        } else {
            echo "   ⚠️ 用户登录测试: " . ($result['response']['message'] ?? '未知错误') . "\n";
        }
    }
    
    private function testFoodApi() {
        echo "4. 测试食材API...\n";
        
        // 测试食材列表
        $result = $this->httpRequest($this->baseUrl . '/food/list?page=1&pageSize=5');
        
        if ($result['code'] === 200 && isset($result['response']['code']) && $result['response']['code'] === 200) {
            echo "   ✅ 食材列表获取成功\n";
        } else {
            echo "   ❌ 食材列表获取失败\n";
        }
        
        // 测试食材搜索
        $result = $this->httpRequest($this->baseUrl . '/food/search?keyword=苹果');
        
        if ($result['code'] === 200 && isset($result['response']['code']) && $result['response']['code'] === 200) {
            echo "   ✅ 食材搜索成功\n";
        } else {
            echo "   ❌ 食材搜索失败\n";
        }
    }
    
    private function testRecipeApi() {
        echo "5. 测试食谱API...\n";
        
        if (!$this->token) {
            echo "   ⚠️ 需要登录token，跳过食谱测试\n";
            return;
        }
        
        $headers = ['Authorization: Bearer ' . $this->token];
        
        // 测试食谱推荐
        $result = $this->httpRequest($this->baseUrl . '/recipe/recommend', 'GET', null, $headers);
        
        if ($result['code'] === 200 && isset($result['response']['code']) && $result['response']['code'] === 200) {
            echo "   ✅ 食谱推荐成功\n";
        } else {
            echo "   ❌ 食谱推荐失败\n";
        }
    }
}

// 运行测试
if (php_sapi_name() === 'cli') {
    $test = new ApiTest();
    $test->runAllTests();
}

?>